//
//  RMMTouchView.m
//  Romomo
//
//  Created by puman on 2017/2/20.
//  Copyright © 2017年 mazegong. All rights reserved.
//

#import "RMMTouch.h"
#import <UIKit/UIKit.h>
#import <LocalAuthentication/LocalAuthentication.h>

@implementation RMMTouch

+ (void)touch_idWith:(void(^)())successful :(void(^)())failure{
        //1. 判断系统版本
        if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
            //2. LAContext : 本地验证对象上下文
            LAContext *context = [LAContext new];
            context.localizedFallbackTitle = @"输入密码";
            context.localizedCancelTitle = @"取消";
            //3. 判断是否可用
            //LAPolicyDeviceOwnerAuthenticationWithBiometrics: 允许设备拥有者使用生物识别技术
            NSError *aotherError = nil;
            NSString *myLocalizedReasonString = @"通过指纹验证来完成付款";
            if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&aotherError]) {
                [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:myLocalizedReasonString reply:^(BOOL success, NSError * _Nullable error) {
                    if (success) {
                        successful();
                    } else {
                        failure();
                        switch (error.code) {
                            case LAErrorAuthenticationFailed:
                            {
                                NSLog(@"用户提供的指纹不对");
                            }
                                break;
                            case LAErrorUserCancel:
                            {
                                NSLog(@"用户点击了取消按钮");
                            }
                                break;
                            case LAErrorUserFallback:
                            {
                                NSLog(@"用户选择输入秘密");
                            }
                                break;
                            case LAErrorSystemCancel:
                            {
                                NSLog(@"切换到其他程序了,被系统取消了");
                            }
                                break;
                            case LAErrorTouchIDLockout:
                            {
                                NSLog(@"用户指纹错误多此,Touch ID被锁定");
                            }
                                break;
                            case LAErrorAppCancel:
                            {
                                NSLog(@"被突如起来的应用打断,比如电话");
                            }
                                break;
    
                            default:
                                break;
                        }
                    }
                }];
    
         } else {
             switch (aotherError.code) {
                 case LAErrorPasscodeNotSet:
                 {
                     NSLog(@"设置里面没有设置密码");
                 }
                     break;
                 case LAErrorTouchIDNotAvailable:
                 {
                     NSLog(@"设备不支持TouchID");
                 }
                     break;
                 case LAErrorTouchIDNotEnrolled:
                 {
                     NSLog(@"设备里面没有设置Touch ID指纹");
                 }
                     break;
                 case LAErrorInvalidContext:
                 {
                     NSLog(@"创建指纹对象失效");
                 }
                     break;
    
                 default:
                     break;
             }
        }
      }

}

@end
